namespace Sustainalytics.Portfolios.BL.Validators
{
    using System.Collections.Generic;

    using Sustainalytics.DataLayer.Shared;
    using Sustainalytics.Entities;
    using Sustainalytics.Utils;
    using Sustainalytics.Utils.Validation.Interfaces;

    public class PortfolioNameValidator : IValidator<Portfolio, string>
    {
        private readonly IRepositoryCollection<Portfolio> _portfolioRepository;

        private readonly PortfolioUserClaims _userClaims;

        public PortfolioNameValidator(
            IRepositoryCollection<Portfolio> portfolioRepository,
            PortfolioUserClaims userClaims)
        {
            _portfolioRepository = portfolioRepository;
            _userClaims = userClaims;
        }

        public bool IsValid(Portfolio entity, string entityAgainstValidate)
        {
            var portfolios = this._portfolioRepository.ReadWhere(p => p.Id != entity.Id && p.Name == entityAgainstValidate && p.AddedByUserId == this._userClaims.UserId);
            return portfolios.IsNullOrEmpty();
        }

        public ICollection<string> BrokenRules(Portfolio entity, string entityAgainstValidate)
        {
            return new[] { "Name already exists" };
        }
    }
}